package com.ruoyi.project.system.controller;

import java.util.List;
import java.util.UUID;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.SysCode;
import com.ruoyi.project.system.service.ISysCodeService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 巡查码Controller
 *
 * @author jjp
 * @date 2020-03-30
 */
@RestController
@RequestMapping("/system/code")
public class SysCodeController extends BaseController {
    @Autowired
    private ISysCodeService sysCodeService;


    /**
     * 查询巡查码列表
     */
    @PreAuthorize("@ss.hasPermi('system:code:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysCode sysCode) {
        startPage();
        List<SysCode> list = sysCodeService.selectSysCodeList(sysCode);
        return getDataTable(list);
    }


    /**
     * 获取巡查码详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:code:query')")
    @GetMapping(value = "/{codeId}")
    public AjaxResult getInfo(@PathVariable("codeId") Long codeId) {
        return AjaxResult.success(sysCodeService.selectSysCodeById(codeId));
    }

    public static String getUUDID8() {
        return UUID.randomUUID().toString().replace("-", "").toLowerCase().substring(0,8);
    }

    /**
     * 新增巡查码
     */
    @PreAuthorize("@ss.hasPermi('system:code:add')")
    @Log(title = "巡查码", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysCode sysCode) {
        String s = UUID.randomUUID().toString().replace("-", "").toLowerCase().substring(0, 8);
        return toAjax(sysCodeService.insertSysCode(sysCode));
    }

    /**
     * 修改巡查码
     */
    @PreAuthorize("@ss.hasPermi('system:code:edit')")
    @Log(title = "巡查码", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysCode sysCode) {
        return toAjax(sysCodeService.updateSysCode(sysCode));
    }

    /**
     * 删除巡查码
     */
    @PreAuthorize("@ss.hasPermi('system:code:remove')")
    @Log(title = "巡查码", businessType = BusinessType.DELETE)
    @DeleteMapping("/{codeIds}")
    public AjaxResult remove(@PathVariable Long[] codeIds) {
        return toAjax(sysCodeService.deleteSysCodeByIds(codeIds));
    }
}
